<template>
  <div class="app-container">
    <el-row type="flex" align="center" :gutter="20">
      <el-form class="filterData-block" label-position="left" :inline="true" label-width="auto">
        <el-form-item label="客户端类型:">
          <el-cascader
            v-model="filterData.userType"
            clearable
            placeholder="全部类型"
            :options="userTypeOption"
            size="small"
            :props="{value:'id',label:'data'}"
            filterDataable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="广告位置:">
          <el-cascader
            v-model="filterData.seat"
            clearable
            placeholder="全部"
            :options="seatOption"
            size="small"
            :props="{value:'id',label:'data'}"
            filterDataable
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="refreshTable">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-plus" type="primary" @click="openDetail(null)">添加广告</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table v-loading="tableConf.loading" :stripe="true" :border="true" :data="tableData">
        <el-table-column align="center" width="50px" prop="id" label="ID"/>
        <el-table-column align="center" min-width="100px" prop="title" label="标题"/>
        <el-table-column align="center" label="图片" width="100px">
          <template slot-scope="{row}">
            <el-image
              style="width: 60px; height: 60px"
              :preview-src-list="[row.imgUrl]"
              :src="row.imgUrl"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="100px" prop="linkUrl" label="链接"/>
        <el-table-column align="center" min-width="100px" prop="userTypeText" label="客户端类型"/>
        <el-table-column align="center" min-width="100px" prop="seatText" label="广告位置"/>
        <el-table-column align="center" width="260px" label="操作">
          <template slot-scope="{row}">
            <el-button size="mini" icon="el-icon-edit-outline" type="primary"  @click="openDetail(row.id)">编辑
            </el-button>
            <el-button size="mini" style="background-color: red;border-color: red;" icon="el-icon-delete" type="primary"  @click="del(row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-pagination
        :page-sizes="tableConf.sizes"
        :page-size="tableConf.size"
        :total="tableConf.total"
        :current-page="tableConf.page"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <el-dialog width="50%"  v-if="visible[0]" :visible.sync="visible[0]" title="添加/编辑" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <Advertisement ref="detail" :id="detailId" :closeDialog="closeDialog"></Advertisement>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="visible[0] = false">取 消</el-button>
        <el-button type="primary" class="primary" @click="$refs.detail.submit()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import Advertisement from '@/components/AdvertisementCode/Detail'

import {deleteAdvertisementByIdApi, getAdminAdvertisementListApi} from "@/api/advertisement";
import {exportDataApi} from "@/api/placeCode";
import {createFile} from "@/utils/utils";

export default {
  components: { Advertisement },
  data() {
    return {
      tableData: [],
      userTypeOption: [
        {id:1,data: '个人'},
        {id:2,data: '企业'},
      ],
      seatOption: [
        {id:1,data: '小程序首页'},
        {id:2,data: 'PC招聘首页位置1'},
        {id:3,data: 'PC招聘首页位置2'},

      ],
      tableConf: {
        page: 1,
        sizes: [10, 20, 50, 100],
        total: 0,
        size: 10,
        loading: false
      },
      visible: {//控制弹窗显示
        0: false
      },
      detailId: null,
      filterData: {
        userType: '',
        seat: ''
      },
      placeTypeLoading: true,

    }
  },
  mounted() {
    this.getTableList()
  },
  methods: {
    downloadCode(url) {
      window.open(url)
    },

    //打开添加/编辑页
    openDetail(id = null) {
      this.detailId = id
      this.visible[0] = true
    },
    //关闭指定弹窗并刷新页面
    closeDialog(index) {
      this.visible[index] = false
      this.getTableList()
    },
    // 每页显示数量change
    handleSizeChange(e) {
      this.tableConf.size = e
      this.getTableList()
    },
    // 当前页change
    handleCurrentChange(e) {
      this.tableConf.page = e
      this.getTableList()
    },
    del(id){
      this.$confirm('确定删除该广告吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        deleteAdvertisementByIdApi({id}).then((res)=>{
          this.$message.success('删除成功');
          this.getTableList()
        })
      })
    },
    //获取列表
    getTableList() {
      this.tableConf.loading = true
      getAdminAdvertisementListApi({
        page: this.tableConf.page,
        limit: this.tableConf.size,
        userType: this.filterData.userType,
        seat: this.filterData.seat
      }).then((res) => {
        this.tableData = res.list
        this.tableConf.total = res.count
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    refreshTable() {
      this.tableConf.page = 1
      this.getTableList()
    }
  }
}
</script>
<style lang="scss" scoped>
.filterData-block {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
